﻿<!doctype html>
<html>
<head>
<#include "/part_meta.ftl">
<#assign store = storeGoods.store />
<title>${storeGoods.name}-${storeGoods.title}-迈小众</title>
<link href="${st.resDomain}/css/style.css" rel="stylesheet" type="text/css">
<link href="${st.resDomain}/css/store.css" rel="stylesheet" type="text/css">
<link href="${st.resDomain}/css/autostyle.css" rel="stylesheet" type="text/css">
<link href="${st.resDomain}/css/store_show.css" rel="stylesheet" type="text/css">
</head>
<body>
<!--头部-->
<header>
  <div class="boxw store">
    <div class="header">
      <span class="f_left"><a href="javascript:history.back();" class="back"></a></span>
      <span class="title">商品详情</span>
      <span class="f_right"><a class="more"></a></span>
    </div>
    <div class="more">
      <a href="${st.domain}/store/${store.storeId}" class="icon-store">店铺主页</a>
      <a href="${st.domain}/goodsClassify?storeId=${store.storeId}" class="icon-search">店内搜索</a>
      <a href="${st.userDomain}/shoppingCart" class="icon-car">购物车</a>
      <a href="${st.userDomain}/" class="icon-user">个人中心</a>
      <a href="${st.domain}/" class="icon-home">主页</a>
    </div>
  </div>
</header>
<!--头部 END-->

<div class="main boxw" style="padding-bottom:4rem;">
  <div class="details">
    <div class="pic"><img src="${st.resDomain}/images/loading.gif" data="${storeGoods.pic}"></div>
    <div class="info">
    	<h2 id="sg_title"><span id="sg_subTitle" style="display:inline;margin-left:3px;color:red;"></span></h2>
      <p><strong id="sg_price"></strong><del id="sg_originalPrice"></del>/<span id="sg_unit"></span></p>
      <#if storeGoods.stock == 0>
       <p style="color:red;">该商品已售完，请选购其他商品</p>
      <#else>
       <p><span>支持：</span> 货到付款 </p>
      <p><span>配送至：</span> 青秀区 <i>有货</i> (库存<span id="sg_stockConvert"></span><span id="sg_sellUnit"></span>)，完成下单后，预计<i>1小时内</i>送达 </p>
      </#if>
    </div>
    <!--- 确认数量 --->
 	<form action="${st.userDomain}/shopping/addDo" method="post" id="shoppingForm">
   	<input type="hidden" name="storeGoods.storeGoodsId" id="storeGoodsId" value="${storeGoods.storeGoodsId}"/>
	<div class="quantify" style="display:block">	  
		<div class="modified">
 				<h4>购买：（<span id="sg_sellUnit"></span>）</h4>
 				<div class="num"><a id="bCountBt_m" class="prv"><!--- none为禁用状态 ---></a><input type="text" name="buyCount" id="buyCount" value="${storeGoods.sellDef}" /><a id="bCountBt_a"  class="add"></a><span>库存：</span><span id="sg_stockConvert"></span><span id="sg_sellUnit"></span></span></div>
		</div>
	</div>
 	</form>
 	<!--- 确认数量 end --->
    <div class="introduce">
    	<a id="xqBt">商品介绍：</a>
    	<div id="xq" class="xq" style="display:none;">无商品介绍</div>
    </div>
    <div class="store">
      <a href="${st.domain}/store/${store.storeId}"><img src="${st.attaDomain}/def/shop_logo.jpg" data="${store.logo}"/><span><h3>${store.name}</h3><h4>好评率：90%</h4></a>
    </div>
    <div class="mainbox">
      <div class="title"><h2>推荐商品</h2></div> 
      <div class="nr">
        <ul class="goodlist f_clear">
        <#list recommandList as g>
          <li><a href="${st.domain}/storeGoods/detail/${g.storeGoodsId}"><span><img src="${st.resDomain}/images/loading.gif" data="${g.pic}" /><#if g.stockConvert lte g.stockEnd><i>已售完</i></#if></span>
            <h3>${g.title}<span style="display:inline;margin-left:3px;color:red">${g.subTitle}</span></h3>
            <h4><strong>￥${g.price}</strong><#if g.atDiscount><del>${g.originalPrice}</del></#if>/${g.unit}</h4>
          </a><#if g.atDiscount><em>特价</em></#if></li>
        </#list>
        </ul>
      </div>
    </div>
  </div>
</div>
<!--尾部-->
<footer class="buy">
  <div class="boxw">
    <nav class="f_clear">
    	<a href="${st.domain}/store/${store.storeId}" class="mxz" style="background-image: url('<#if store.logo??>${st.attaDomain}${store.logo}<#else>${st.attaDomain}/def/shop_logo.jpg</#if>');"></a>
    	<a href="${st.userDomain}/shoppingCart" class="shoppingcar"><span id="shoppingCount">0</span></a><a class="add" id="addToCartBt">加入购物车</a>
    </nav>
  </div>
</footer>
<!--尾部-->
<script src="${st.resDomain}/scripts/jquery-1.9.1.min.js"></script>
<script src="${st.resDomain}/scripts/com.js"></script>
<script>

(function($) {
	var detailPath_ = g_domain + "/storeGoods/detail/json";
	var countPath_ = g_userDomain + "/shopping/count/json";
	var sg_ = null;// storeGoods
	var hbCount_ = -1;
	var storeGoodsId_ = $("#storeGoodsId").val();
	var $form_ = $("#shoppingForm");
	var $buyCount_ = $("#buyCount");
	var init_ = function() {
		$.post(detailPath_, { storeGoodsId: storeGoodsId_},
		function(data) {
			if (data.status !== 0) {
				showMsg(data.message, 'error');
				return;
			}
			sg_ = data.result;// storeGoods
			for (var key in sg_) {
				$("[id^='sg_" + key + "']").html(sg_[key]);
			}
			$("[id='sg_price']").html("￥" + sg_.price);
			$("[id='sg_originalPrice']").html(sg_.atDiscount?sg_.originalPrice:"");
			$("[id='sg_sellUnit']").html(sg_.sellUnit?sg_.sellUnit:sg_.unit);
			
			$("#xq").html(sg_.description);
			
		});
	};
	var bCountBtEvent_ = function(event) {
		var $bCountBt = $(event.currentTarget);
		var vars = $bCountBt.attr("id").split("_");
		var btType = vars[1];
		var preCount = parseFloat($buyCount_.val());
		if (isNaN(preCount)) {
			showMsg("请填写数字！", 'error');
			return;
		}
		$buyCount_.val(preCount);
		var destCount = sg_.sellDef;
		// 减
		if (btType == "m") {
			destCount = preCount - sg_.sellExt;
			if (destCount < sg_.sellMin) {
				destCount = sg_.sellMin;
			}
		
		} // 加
		else {
			destCount = preCount + sg_.sellExt;
		}
		if(destCount.toString().indexOf(".") > 0){
			// 小数计算保留一位小数
			destCount = destCount.toFixed(1);
		}
		$buyCount_.val(destCount);
		$buyCount_.trigger("change");
	};
	var bCountChangeEvent_ = function(event, okCallback) {
		var buyCount = parseFloat($buyCount_.val());
		if (isNaN(buyCount)) {
			showMsg("请填写数字！", 'error');
		}

		if (sg_.stockConvert <= 0) {
			$buyCount_.val(0);
			showMsg("已无可购买库存！", 'error');
		} else if (buyCount <= 0) {
			$buyCount_.val(0);
			showMsg("购买量不能为0", 'error');
		} else {
			if (hbCount_ == -1) {
				$.post(countPath_,{storeGoodsId: sg_.storeGoodsId},function(data) {
					if (data.status !== 0) {
						showMsg(data.message, 'error');
						return;
					}
					hbCount_ = data.result;
					checkAddCount(buyCount, okCallback);
				})
			} else {
				checkAddCount(buyCount, okCallback);
			}
		}
		
		function checkAddCount(buyCount, okCallback) {
			if (sg_.stockConvert < (hbCount_ + buyCount)) {
				buyCount = sg_.stockConvert - hbCount_;
				$buyCount_.val(buyCount);
				var msg = "最多还可购买：" + buyCount + (sg_.sellUnit?sg_.sellUnit:sg_.unit);
				if (hbCount_ > 0) {
					msg = "购物车中已有数量：" + hbCount_ + (sg_.sellUnit?sg_.sellUnit:sg_.unit) + "！" + msg
				}
				showMsg(msg, 'error');
				return;
			}
			$buyCount_.val(buyCount);
			okCallback ? okCallback(): void(0);
		}		
	};
	var addToCartEvent_ = function() {
		if (sg_.stockConvert == 0) {
	        showMsg("已售完，选购其他商品吧", 'error');
	        return;
	    }
		Login.checkLogin(function() {
			bCountChangeEvent_(null, function() {
				$form_.submit();
			});
		});
	};
	var xqBtEvent_ = function() {
		$("#xq").toggle();
	}
	
	// 加载商品信息
	init_();
	
	// 事件:加/减购买量
	$(document).on('click',"[id^='bCountBt_']", bCountBtEvent_);
	// 事件:输入
	$(document).on('blur',"#buyCount", bCountChangeEvent_);
	// 事件:购买量变化
	$(document).on('change',"#buyCount", bCountChangeEvent_);
	// 事件：加入购物车
	$(document).on('click',"#addToCartBt", addToCartEvent_);
	// 商品介绍
	$(document).on('click',"#xqBt", xqBtEvent_);

})(jQuery);

</script>
</body>
</html>
